using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The StandarLevelClassRoomLocationTableDB class is responsible for interacting with the database to retrieve and store information
/// about StandarLevelClassRoomLocationTable objects.
/// </summary>
public class StandarLevelClassRoomLocationTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of StandarLevelClassRoomLocationTable from the underlying datasource.
/// </summary>
/// <param name="standarLevelKey">The unique StandarLevelKey of the StandarLevelClassRoomLocationTable in the database.</param>
/// <returns>An StandarLevelClassRoomLocationTable when the StandarLevelKey was found in the database, or null otherwise.</returns>
public static StandarLevelClassRoomLocationTable GetItem(int standarLevelKey)
{
StandarLevelClassRoomLocationTable myStandarLevelClassRoomLocationTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelClassRoomLocationTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@standarLevelKey", standarLevelKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myStandarLevelClassRoomLocationTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myStandarLevelClassRoomLocationTable;
}
}

/// <summary>
/// Returns a list with StandarLevelClassRoomLocationTable objects.
/// </summary>
/// <returns>A generics List with the StandarLevelClassRoomLocationTable objects.</returns>
public static StandarLevelClassRoomLocationTableList GetList()
{
StandarLevelClassRoomLocationTableList tempList = new StandarLevelClassRoomLocationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelClassRoomLocationTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a StandarLevelClassRoomLocationTable in the database.
/// </summary>
/// <param name="myStandarLevelClassRoomLocationTable">The StandarLevelClassRoomLocationTable instance to save.</param>
/// <returns>The new StandarLevelKey if the StandarLevelClassRoomLocationTable is new in the database or the existing StandarLevelKey when an item was updated.</returns>
public static int Save(StandarLevelClassRoomLocationTable myStandarLevelClassRoomLocationTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelClassRoomLocationTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myStandarLevelClassRoomLocationTable.StandarLevelKey == -1)
{myCommand.Parameters.AddWithValue("@standarLevelKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@standarLevelKey", myStandarLevelClassRoomLocationTable.StandarLevelKey);
}
if (string.IsNullOrEmpty(myStandarLevelClassRoomLocationTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myStandarLevelClassRoomLocationTable.Remark);
}
if (myStandarLevelClassRoomLocationTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myStandarLevelClassRoomLocationTable.Status);
}
if (myStandarLevelClassRoomLocationTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myStandarLevelClassRoomLocationTable.CreatedDateTime);
}
if (myStandarLevelClassRoomLocationTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myStandarLevelClassRoomLocationTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a StandarLevelClassRoomLocationTable from the database.
/// </summary>
/// <param name="standarLevelKey">The StandarLevelKey of the StandarLevelClassRoomLocationTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int standarLevelKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStandarLevelClassRoomLocationTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@standarLevelKey", standarLevelKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the StandarLevelClassRoomLocationTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static StandarLevelClassRoomLocationTable FillDataRecord(IDataRecord myDataRecord )
{
StandarLevelClassRoomLocationTable myStandarLevelClassRoomLocationTable = new StandarLevelClassRoomLocationTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LocationKey")))
{
myStandarLevelClassRoomLocationTable.LocationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("LocationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ClassRoomKey")))
{
myStandarLevelClassRoomLocationTable.ClassRoomKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("ClassRoomKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StandarLevelKey")))
{
myStandarLevelClassRoomLocationTable.StandarLevelKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StandarLevelKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myStandarLevelClassRoomLocationTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myStandarLevelClassRoomLocationTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myStandarLevelClassRoomLocationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myStandarLevelClassRoomLocationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myStandarLevelClassRoomLocationTable;
}
}

 } 
